const aiTitleDemo = {
  meMemorialId: 'xxx',
  me: 'xxx',
  him: 'xxx',
  type: 1
  // type: 2
}

const aiTitleListDemo = [{
  meMemorialId: 'xxx',
  me: 'xxx',
  him: 'xxx',
  talkList: [{
      id: 'xxx',
      content: 'xxx',
      pic: 'xxx',
      type: 1
    },
    {
      id: 'xxx',
      content: 'xxx',
      pic: 'xxx',
      type: 2
    }
  ]
}]

const aiExp = 60 * 60 * 24 * 1e3

const getItem = (meMemorialId) => {
  const aiTitleList = uni.getStorageSync('aiTitleList') || []
  return aiTitleList.find(o => o.meMemorialId === meMemorialId)
}

const setItem = (obj) => {
  const newTime = new Date().getTime()
  if (typeof obj === 'object') {
    const aiTitleList = uni.getStorageSync('aiTitleList') || []
    const aiTitleIndex = aiTitleList.findIndex(o => o.meMemorialId === obj.meMemorialId)
    if (aiTitleIndex !== -1) {
      Object.assign(aiTitleList[aiTitleIndex], obj)
    } else {
      aiTitleList.push(Object.assign(obj, {
        talkList: []
      }))
    }

    // 清理过期的记录
    aiTitleList.forEach(aiTitle => {
      aiTitle.talkList = aiTitle.talkList.filter(o => newTime - Number(o.id) < aiExp)
    })

    uni.setStorageSync('aiTitleList', aiTitleList)
  }
}

export {
  aiTitleListDemo,
  aiTitleDemo,
  aiExp,
  getItem,
  setItem,
}